<?php
namespace Home\Controller;
class UserController extends CommonController {
	public function __construct(){
		parent::__construct();
		$allow_action = array(
		             'login','register'
		             );
		             if ($this->userInfo === false && !in_array(ACTION_NAME, $allow_action)) {
		             	$this->error('请先登录。',U('User/login'));
		             }
	}
	//用户登录
	public function login(){
		//    	if (IS_POST) {
		//    		$user=M('user');//实例化模型表
		//    		//获取登录用户信息
		//    		$user_Id=I('post.userId');
		//    		$pwd=I('post.password');
		//    		//检索数据库user表 并判断是否存在该用户 账号
		//    		$userId=$user->where('UserId='.$user_Id)->getField('UserId');
		//    		$password=$user->where('UserId='.$user_Id)->getField('password');
		//    		$userName=$user->where('UserId='.$user_Id)->getField('UserName');
		//    		//如果账号不存在 、输入密码错误
		//    		if ($userId == "") {
		//    			$this->error('登录失败，用户名不存在...',U('User/login'));
		//    		}elseif ($pwd != $password){
		//    			$this->error('登录失败，密码错误...',U('User/login'));
		//    		}elseif ($user_Id == $userId && $pwd == $password) {
		//    			//获取登录用户id和昵称
		//    			session('userId',$userId);
		//    		    session('userName',$userName);
		//    			$this->success('小米商城欢迎您~',U('Index/index'));
		//    			return;
		//    		}
		//    	}

		if (IS_POST){
			$userId=I('post.userId');
			$password=$this->password(I('post.password'));
			$rst=D('user')->checkUser($userId,$password); //为用户登录构造一个方法检查登录->userModel
			if ($rst!==true) {
				$this->error($rst);
			}
			$this->success('小米商城欢迎您~',U('Index/index'));
			return ;
		}
		$this->display();
	}

	public function register(){
		if (IS_POST) {
			$user=M('user');//实例化模型表;
			//判断该注册用户是否存在
			$data=$user->create();
			$user_Id=intval($data['UserId']);
			$userId=$user->where('UserId='.$user_Id)->getField('UserId');
			if ($userId == $user_Id) {
				$this->error('注册失败，该用户名已经存在...',U('User/register'));
			}else{
				$res=$user->add();//使用add方法添加到注册表
				if ($res) {
					$password=$user->where('UserId='.$user_Id)->getField('Password');
					$pwd['Password']=$this->password($password);
					$user->where('UserId='.$user_Id)->save($pwd);
					$this->success('注册成功...',U('User/login'));
					return ;
				}else{
					$this->error('注册失败...',U('User/register'));
				}
			}
		}
		$this->display();
	}

	//提交订单
	public function addorder(){
		$orderdata['UserId']=$this->userInfo['userId'];
		$orderdata['Ordercol']=sizeof($_POST['goodids']);
		//获取用户信息
		$data=M('user')->where('UserId='.$orderdata['UserId'])->field('UserName,Phone,Address')->find();
		//向order表添加数据源
		$orderdata['ImportAddress']=I("post.ImportAddress",$data['Address']);
		$orderdata['PickPhone']=I("post.PickPhone",$data['Phone']);
		$orderdata['PickName']=I("post.PickName",$data['UserName']);
		$$orderdata['AdministratorId']=1090;
		$orderdata['Remark']=I("post.Remark","");
		date_default_timezone_set('PRC');
		$orderdata['BuyDate']=date('Y-m-d H:i:s');
		$orderdata['TotalMoney']=I('post.TotalMoney');
		$orderdata['OrderStatus']="0";
			
		$orderdetaildata['order_OrderId']=M('order')->add($orderdata);
		if(!$orderdetaildata['order_OrderId']){
			$this->error("提交订单失败");
		}

		//--------------------------------------------------------------
		$where=array();
		$subwhere=array();
		//		$orderdetaildata=array();
		foreach ($_POST['goodids'] as $v){
			$subwherex=explode('@@@', $v);
			$subwhere['GoodId']=$subwherex[0];
			$subwhere['Color']=$subwherex[1];
			$subwhere['Version']=$subwherex[2];

			$good=M('goods')->where($subwhere)->find();
			$orderdetaildata['GoodCount']=$subwherex[3];
			$orderdetaildata['goods_GoodId']=$subwhere['GoodId'];
			$orderdetaildata['goods_Color']=$subwhere['Color'];
			$orderdetaildata['goods_Version']=$subwhere['Version'];
			$goodsdata=M('goods')->where($subwhere)->find();
			$orderdetaildata['GoodName']=$goodsdata['GoodName'];
			$f=M('orderdetail')->add($orderdetaildata);	//添加商品到orderdetail表中

			//	减少下单后 商品的数量，若为0，则自动下架
			$new['Number']=$goodsdata['Number']-$orderdetaildata['GoodCount'];
			if($new['Number']==0)$new['Status']=2;
			M("goods")->where($subwhere)->save($new);
			$subwhere=array();
			if(!$f){
				M('order')->where("OrderId=".$orderdetaildata['order_OrderId'])->delete();
				$this->error("提交订单失败");
			}
			$cartwhere['goods_GoodId']=$orderdetaildata['goods_GoodId'];
			$cartwhere['goods_Color']=$orderdetaildata['goods_Color'];
			$cartwhere['goods_Version']=$orderdetaildata['goods_Version'];
			M('cart')->where("user_UserId=".$orderdata['UserId'])->where($cartwhere)->delete();
				
		}
		$this->success('提交订单成功',U('pay'));
	}

	//订单页面显示

	public function pay(){		  //订单付款
		$orderwhere['UserId']=$this->userInfo['userId'];
		$orderwhere['OrderStatus'] = 0;
		$ordersdata=M('order')->order('OrderId')->where($orderwhere)->select();
		$orderdetaildata=array();

		for($i=0;$i<sizeof($ordersdata);$i++){
			$orderdetailsdata=M('orderdetail')->join("join goods ON orderdetail.goods_GoodId=goods.GoodId")->where("orderdetail.goods_Color=goods.Color and orderdetail.goods_Version=goods.Version and order_OrderId=".$ordersdata[$i]['OrderId'])->select();
			array_push($orderdetaildata,$orderdetailsdata);
		};
		$this->assign('orderitem',$ordersdata);
		$this->assign('orders',$orderdetaildata);
		$this->display();
			
	}



	public function payorder(){
		$orderdata['OrderId']=I("get.OrderId");
		$orderstatus['OrderStatus'] = 1;
		M('order')->where($orderdata)->save($orderstatus);
		$this->success("付款成功",U('receipt'));
	}



	public function receipt()		 //确认收货
	{
		// TODO: implement

		//订单付款
		$orderwhere['UserId']=$this->userInfo['userId'];
		$orderwhere['OrderStatus'] = 1;
		$ordersdata=M('order')->order('OrderId')->where($orderwhere)->select();
		$orderdetaildata=array();

		for($i=0;$i<sizeof($ordersdata);$i++){
			$orderdetailsdata=M('orderdetail')->join("join goods ON orderdetail.goods_GoodId=goods.GoodId")->where("orderdetail.goods_Color=goods.Color and orderdetail.goods_Version=goods.Version and order_OrderId=".$ordersdata[$i]['OrderId'])->select();
			array_push($orderdetaildata,$orderdetailsdata);
		};
		$this->assign('orderitem',$ordersdata);
		$this->assign('orders',$orderdetaildata);
		$this->display();
	}


	/**
	 * @return   void
	 */
	public function confirmReceipt()		 //确认收货
	{
		// TODO: implement
		$orderwhere['UserId']=$this->userInfo['userId'];
		$orderwhere['OrderStatus'] = 2;
		$ordersdata=M('order')->order('OrderId')->where($orderwhere)->select();
		$orderdetaildata=array();

		for($i=0;$i<sizeof($ordersdata);$i++){
			$orderdetailsdata=M('orderdetail')->join("join goods ON orderdetail.goods_GoodId=goods.GoodId")->where("orderdetail.goods_Color=goods.Color and orderdetail.goods_Version=goods.Version and order_OrderId=".$ordersdata[$i]['OrderId'])->select();
			array_push($orderdetaildata,$orderdetailsdata);
		};
		$this->assign('orderitem',$ordersdata);
		$this->assign('orders',$orderdetaildata);
		$this->display();
	}

	public function confirmReceiptYes(){
		$orderdata['OrderId']=I("get.OrderId");
		$orderstatus['OrderStatus'] = 3;
		$rst=M('order')->where($orderdata)->save($orderstatus);
		if(rst)
		$this->success('确认收货成功！',U('confirmReceipt'));
	}

	//个人信息显示页面
	public function personalInfo(){
		//修改信息
		if (IS_POST) {
			$userId = $this->userInfo['userId'];
			$model = M('user');
			if(I('post.pwd')!='')$data['Password']=$this->password(I('post.pwd'));
			$data['UserName']=I('post.nickName','小白');
			$data['Gender']=I('post.gender','男');
			$data['Age']=I('post.age',0);
			$data['Phone']=I('post.phone','');
			$data['Email']=I('post.email','');
			$data['Address']=I('post.address','');
			$res = $model->where("userId=$userId")->save($data);
			if ($res) {
				$this->success("修改成功",U('User/personalInfo'));
				session('userName',$data['UserName']);
				return ;
			}else{
				$this->error("修改失败",U('User/personalInfo'));
				return ;
			}
		}
		$userId = $this->userInfo['userId'];
		//检索用户信息
		$data['info'] = D('user')->where('UserId='.$userId)->find();
		//var_dump("");
		$this->assign($data);
		$this->display();
	}

	//显示商品评价表
	public function appraiseindex(){
		$orderwhere['UserId']=$this->userInfo['userId'];
		$orderwhere['OrderStatus'] = 3;
		$ordersdata=M('order')->order('OrderId')->where($orderwhere)->select();
		$orderdetaildata=array();

		for($i=0;$i<sizeof($ordersdata);$i++){
			$orderdetailsdata=M('orderdetail')->join("join goods ON orderdetail.goods_GoodId=goods.GoodId")->where("orderdetail.goods_Color=goods.Color and orderdetail.goods_Version=goods.Version and xing is NULL and order_OrderId=".$ordersdata[$i]['OrderId'])->select();
			array_push($orderdetaildata,$orderdetailsdata);
		};
		$this->assign('orderitem',$ordersdata);
		$this->assign('orders',$orderdetaildata);
		$this->display();
	}

	//子商品评价表
	public function appraise(){
		$where=I('get.');
		$orderid=I('get.order_OrderId');
		$where['order_OrderId']=$orderid;
		$sss['OrderStatus']=4;
		$data['o']=M('orderdetail')->where($where)->find();
		//		var_dump($where);
		//		$goodId = I('get.gid');
		//		$userId = $this->userInfo['userId'];
		//		$data['od'] = D('orderdetail')->getlist($userId,$goodId);
		$this->assign($data);
		//var_dump($goodId);
		//提交商品评价表
		if (IS_POST) {
			$save['xing']=I('post.sel','★★★★★');
			$save['PingJia']=I('post.app','');
			//$orderId = D('order')->getOrderId($userId); //获取订单id 数组类型不可以
			M('orderdetail')->where($where)->save($save);
			if(!M('orderdetail')->where("order_OrderId=$orderid and xing is NULL")->select())
			M('order')->where("OrderId=".$orderid)->save($sss);
			$this->success('谢谢亲的评价~',U('User/appraiseindex'));
			return;
		}
		$this->display();
	}

	public function over(){
		$where['UserId']= $this->userInfo['userId'];
		$where['OrderStatus']=4;
		$data['ors']=M('order')->join("right join orderdetail ON orderdetail.order_OrderId=order.OrderId join goods ON orderdetail.goods_GoodId=goods.GoodId")->where('orderdetail.goods_Version=goods.Version')->where('orderdetail.goods_Color=goods.Color')->where($where)->select();
		$this->assign($data);
		$this->display();
	}


	public function delOrder(){
		$orderdetailwhere['order_OrderId']=I('get.OrderId');
		$orderwhere['OrderId']=I('get.OrderId');
		$orderdetaildata=M('orderdetail')->where($orderdetailwhere)->select();
		$goodwhere=array();
		foreach ($orderdetaildata as $v){
				
			$goodwhere['GoodId']=$v['goods_GoodId'];
			$goodwhere['Color']=$v['goods_Color'];
			$goodwhere['Version']=$v['goods_Version'];
				
			$goodsdata=M('goods')->where($goodwhere)->find();
			$new['Number']=$goodsdata['Number']+$v['GoodCount'];
			$new['Status']=1;
			M("goods")->where($goodwhere)->save($new);
			$goodwhere=array();
		}
		M('orderdetail')->where($orderdetailwhere)->delete();
		$orderdata=M('order')->where($orderwhere)->find();
		M('order')->where($orderwhere)->delete();
		$this->success("取消订单成功",U('pay'));
	}



}

